package com.zhou.share;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class MainActivity extends Activity implements View.OnClickListener{
//    private TextView tv_tip1;
//    private TextView tv_tip2;
    private LinearLayout ll_box;
    private Button btn_refresh,btn_pre_week,btn_next_week;
    private EditText et_phone;
    private SharedPreferences sharedPreferences;
    private String phone;
    private long nowTime;
    private long h = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sharedPreferences = getSharedPreferences("phone", Context.MODE_PRIVATE);
        phone = sharedPreferences.getString("phone", "");
        nowTime = System.currentTimeMillis();
        initView();
        if(!TextUtils.isEmpty(phone)){
            et_phone.setText(phone);
        }else{
            Toast.makeText(this,"请输入您要查询的手机号！",Toast.LENGTH_LONG).show();
        }
        int i = getI();
        getData(getUrl(i));

    }

    private String getUrl(int i){
        String monday = new SimpleDateFormat("yyyy-MM-dd").format(nowTime-(i-1)*1000*60*60*24);
        String sunday = new SimpleDateFormat("yyyy-MM-dd").format(nowTime+(7-i)*1000*60*60*24);
        Log.e("---------->","http://d3cms.zhongsou.com/souyueshare/Query.aspx?kw=18210956260&begin="+monday+"&end="+sunday+"  "+i);
        return "http://d3cms.zhongsou.com/souyueshare/Query.aspx?kw="+phone+"&begin="+monday+"&end="+sunday;
    }

    private int getI(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(nowTime);
        int i = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if(i == 0){
            return 7;
        }else{
            return i;
        }
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.btn_refresh:
                if(TextUtils.isEmpty(et_phone.getText().toString())){
                    Toast.makeText(this,"请输入您要查询的手机号！",Toast.LENGTH_LONG).show();
                }else{
                    sharedPreferences.edit().putString("phone",et_phone.getText().toString()).commit();
                    phone = et_phone.getText().toString();
                    ll_box.setVisibility(View.VISIBLE);
                    getData(getUrl(getI()));
                }
                ;break;
            case R.id.btn_pre_week:
                    h--;
                    nowTime=System.currentTimeMillis()+1000*60*60*24*7*h;
                    Log.e("---->",1000*60*60*24*7*h+"   h="+h);
                getData(getUrl(getI()));
                break;
            case R.id.btn_next_week:
                if(h<0){
                    h++;
                    nowTime=System.currentTimeMillis()+1000*60*60*24*7*h;
                    getData(getUrl(getI()));
                }else{
                    Toast.makeText(this,"当前正是本周统计数据！",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    private interface GetResult{
        void getResult(ArrayList<String> result);
    }
    private void getData(final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect(url).get();
                    Elements titleElements = document.body().getElementsByClass("table_b_tit");
                    Elements valueElements = document.body().getElementsByClass("td_gray");

                    String monday = new SimpleDateFormat("yyyy-MM-dd").format(nowTime-(getI()-1)*1000*60*60*24);
                    String sunday = new SimpleDateFormat("yyyy-MM-dd").format(nowTime+(7-getI())*1000*60*60*24);
                    ArrayList<String> arr = new ArrayList<String>();
                    arr.add("日期："+monday+"  ->  "+sunday);
                    for(int i=0;i<valueElements.size();i++){
                        TextNode nodeTitle = (TextNode)titleElements.get(i).childNodes().get(0);
                        TextNode nodeValue = (TextNode)valueElements.get(i).childNodes().get(0);
                        arr.add(nodeTitle.text()+":"+nodeValue.text());
                    }
                    Message msg = new Message();
                    msg.obj = arr;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView(){
//        tv_tip1 = (TextView)findViewById(R.id.tv_tip1);
//        tv_tip2 = (TextView)findViewById(R.id.tv_tip2);
        ll_box = (LinearLayout)findViewById(R.id.ll_box);
        btn_refresh = (Button)findViewById(R.id.btn_refresh);
        btn_refresh.setOnClickListener(this);
        btn_pre_week =(Button)findViewById(R.id.btn_pre_week);
        btn_next_week = (Button)findViewById(R.id.btn_next_week);
        et_phone = (EditText)findViewById(R.id.et_phone);
        btn_pre_week.setOnClickListener(this);
        btn_next_week.setOnClickListener(this);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){
                ArrayList<String> result=(ArrayList<String>) msg.obj;
                ll_box.removeAllViews();
                for(int i=0;i<result.size();i++){
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    TextView textView = new TextView(MainActivity.this);
                    textView.setText(result.get(i));
                    textView.setTextColor(getResources().getColor(R.color.red));
                    textView.setLayoutParams(params);
                    ll_box.addView(textView);
                }
                ll_box.setVisibility(View.VISIBLE);
            }
        }
    };
}
